package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ck
 * @date 2021/10/17 23:16
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDis(setmealDto);
        return R.success("新增套餐成功");
    }


    @Cacheable(value = "setmealCache",key = "#page+'_'+#pageSize")
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page> page(int page, int pageSize, String name) {
        R<Page> pageR = setmealService.pageCat(page, pageSize, name);
        return pageR;
    }

    @DeleteMapping
    //allEntries 表示删除setmealCache下面所有的缓存数据
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    public R<String> updateWithStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        setmealService.updateWithStatus(status, ids);
        return R.success("修改成功");
    }

    /**
     * 根据条件来查询套餐数据
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);


        return R.success(list);
    }

    @Cacheable(value = "setmealCache",key = "#id")
    @GetMapping("/{id}")
    @ApiOperation("根据id获取套餐数据接口")
    public R<SetmealDto> get(@PathVariable Long id) {
        R<SetmealDto> withId = setmealService.getWithId(id);
        return withId;
    }

    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    @Transactional
    @ApiOperation("修改套餐接口")
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateData(setmealDto);
        return R.success("修改成功");
    }

    @Cacheable(value = "setmealCache",key = "#id")
    @GetMapping("/dish/{id}")
    @ApiOperation("获取套餐中菜品数据接口")
    public R<List<DishDto>> dish(@PathVariable Long id) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        List<DishDto> dishDto = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            DishDto dto = new DishDto();
            LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Dish::getId,setmealDish.getDishId());
            Dish dish = dishService.getOne(queryWrapper1);
            dto.setCopies(setmealDish.getCopies());
            BeanUtils.copyProperties(dish,dto);
            dishDto.add(dto);
        }

        return R.success(dishDto);
    }
}
